package com.orcamento.entity.report;

import org.joda.time.DateTime;

import com.orcamento.entity.Fund;
import com.orcamento.entity.Gain;
import com.orcamento.entity.Spent;
import com.orcamento.entity.enums.Month;
import com.orcamento.util.CalendarUtil;
import com.orcamento.util.MathUtil;
import com.orcamento.util.Money;

public class OverviewReport extends Report {
	
	public OverviewReport(Month month, Integer year) {
		super(month, year);
	}
	
	@Override
	public void generate() {
		excludeCreditCardSpents();
	}

	public Money getTotalGain() {
		Money totalGain = new Money();
		for( Gain gain : this.gains ) {
			totalGain = totalGain.plus( gain.getValue() );
		}
		return totalGain;
	}
	
	public Money getTotalSpent() {
		Money totalSpent = new Money();
		for( Spent spent : this.spents ) {
			totalSpent = totalSpent.plus( spent.getValue() );
		}
		return totalSpent;
	}
	
	public Money getTotalFundStartBalance() {
		Money money = new Money();
		for(Fund fund : funds) {
			money = money.plus( fund.getStartBalance() );
		}
		return money;
	}

	public Money getTotalFundFinalBalance() {
		Money money = new Money();
		for(Fund fund : funds) {
			money = money.plus( fund.getFinalBalance() );
		}
		return money;
	}
	
	public Money getTotalFundsEvolution() {
		Money money = new Money();
		for(Fund fund : funds) {
			money = money.plus( fund.getEvolution() );
		}
		return money;
	}
	
	public String getTotalFundsEvolutionPercent() {
		return MathUtil.getMoneyPercentage(getTotalFundsEvolution(), getTotalFundStartBalance());
	}

	public Money getLastMonthTotalGain() {
		Month lastMonth = month.previous();
		DateTime startDate = CalendarUtil.toDate(1, lastMonth.id, year, 0, 0, 0);
		DateTime endDate = CalendarUtil.toDate(lastMonth.maxDays, lastMonth.id, year, 23, 59, 59);
		Money totalGainLastMonth = new Money();
		
		for( Fund fund : funds ) {
			totalGainLastMonth = totalGainLastMonth.plus( fund.getTotalGainsBetweenDates(startDate, endDate) );
		}
		
		return totalGainLastMonth;
	}

	public Money getMonthTotalGain() {
		DateTime startDate = CalendarUtil.toDate(month.previous().maxDays, month.previous().id, year, 0, 0, 0);
		DateTime endDate = CalendarUtil.toDate(month.maxDays, month.id, year, 23, 59, 59);
		Money totalGainCurrentMonth = new Money();
		
		for( Fund fund : funds ) {
			totalGainCurrentMonth = totalGainCurrentMonth.plus( fund.getTotalGainsBetweenDates(startDate, endDate) );
		}
		
		return totalGainCurrentMonth;
	}

	public String getEvolutionGainPercentageComparingLastMonth() {
		Money lastMonthTotalGain = getLastMonthTotalGain();
		Money currentMonthTotalGain = getMonthTotalGain();
		Money evolution = currentMonthTotalGain.minus(lastMonthTotalGain);
		return MathUtil.getMoneyPercentage(evolution, lastMonthTotalGain);
	}

	public Money getLastMonthTotalSpent() {
		Month lastMonth = month.previous();
		DateTime startDate = CalendarUtil.toDate(lastMonth.previous().maxDays, lastMonth.previous().id, year, 0, 0, 0);
		DateTime endDate = CalendarUtil.toDate(lastMonth.maxDays, lastMonth.id, year, 23, 59, 59);
		Money totalSpentsLastMonth = new Money();
		
		for( Fund fund : funds ) {
			totalSpentsLastMonth = totalSpentsLastMonth.plus( fund.getTotalSpentsBetweenDates(startDate, endDate) );
		}
		
		return totalSpentsLastMonth;
	}

	public Money getMonthTotalSpent() {
		DateTime startDate = CalendarUtil.toDate(1, month.id, year, 0, 0, 0);
		DateTime endDate = CalendarUtil.toDate(month.maxDays, month.id, year, 23, 59, 59);
		Money totalSpentCurrentMonth = new Money();
		
		for( Fund fund : funds ) {
			totalSpentCurrentMonth = totalSpentCurrentMonth.plus( fund.getTotalSpentsBetweenDates(startDate, endDate) );
		}
		
		return totalSpentCurrentMonth;
	}

	public String getEvolutionSpentPercentageComparingLastMonth() {
		Money lastMonthTotalSpent = getLastMonthTotalSpent();
		Money currentMonthTotalSpent = getMonthTotalSpent();
		Money evolution = currentMonthTotalSpent.minus(lastMonthTotalSpent);
		
		return MathUtil.getMoneyPercentage(evolution, lastMonthTotalSpent);
	}

}
